package com.ww.springboot.boot.baseTest.集合;

import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 描述：
 *
 * @author wanwei
 * @since 2021-08-21 17:59
 */
public class CollectionUtilDemo {
    public static void main(String[] args) {
        Collections.synchronizedCollection(new ArrayList<>());
        Collections.synchronizedList(new ArrayList<>());
        Collections.synchronizedMap(new HashMap());
        Collections.synchronizedSet(new HashSet());

        ConcurrentHashMap<String, String> map = new ConcurrentHashMap();
        map.put("1", "1");
        //保证相同的key只加载一次
        //如果没有值 就塞值 有就不塞了
        map.computeIfAbsent("2", key -> key + "3");
        //两个的区别是compute的value 可以带上key 并进行运算
        map.putIfAbsent("2", "3");
        System.out.println(map);

        map.computeIfPresent("4", (key,value) -> {
            System.out.println(key);
            System.out.println(value);
            return value + 3;
        });

        System.out.println(map);


        Map<String, BigDecimal> skuCostMap = new HashMap<>();

        skuCostMap.computeIfAbsent("2", key -> new BigDecimal(100));
        skuCostMap.computeIfPresent("2", (key,value) -> value.add(new BigDecimal(100)));
        skuCostMap.computeIfAbsent("2", key -> new BigDecimal(100));
        skuCostMap.computeIfPresent("2", (key,value) -> value.add(new BigDecimal(100)));

        System.out.println(skuCostMap);
    }
}
